package com.submit.demo.controller;

import com.submit.demo.annotation.AuthIgnore;
import com.submit.demo.common.api.CommonResult;
import com.submit.demo.config.MinIOProperties;
import com.submit.demo.dto.WordsDownloadParam;
import com.submit.demo.service.FileService;
import com.submit.demo.util.MinIOUtil;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("file")
@Slf4j
public class FileController {
    @Autowired
    private FileService fileService;

    @Autowired
    private MinIOProperties minIOProperties;

    @AuthIgnore
    @ApiOperation("下载文档")
    @PostMapping("download")
    public void downTaskFile(HttpServletResponse response, @RequestParam("filename") String filename) {
        Assert.notNull(filename);
        log.info("正在下载" + filename);
        fileService.download(response, filename);
    }

    @AuthIgnore
    @ApiOperation("批量下载提交人文档")
    @PostMapping("downFiles")
    public void download(HttpServletResponse response, WordsDownloadParam param) {
        Assert.notNull(param);
        log.info("正在批量下载" + param.getNames());
        fileService.zipDownload(response, param.getNames());
    }

    @ApiOperation("获取文件下载地址")
    @PostMapping("getFileUrl")
    public CommonResult<String> getUrl(String name){
        return CommonResult.success(MinIOUtil.getImageUrl(minIOProperties.getBucketName(), name));
    }

}
